import 'package:flutter/material.dart';
import 'package:project_service_app/Field/LevelStart/StarItemField.dart';

import 'model/LevelModel.dart';

class StarLevelField extends StatefulWidget {
  final ImageProvider img;
  final Color selected, unselected;
  final int quantity;
  final double size, select, fill;
  final bool tap;
  final LevelController controller;
  StarLevelField({
    Key key,
    this.img,
    this.size,
    this.select=0,
    this.quantity=5,
    this.selected=Colors.yellow,
    this.unselected=Colors.grey,
    this.controller,
    this.tap=true,
    this.fill=0.7}) : super(key: key);

  @override
  State<StatefulWidget> createState() => StarLevelFieldState();
}

class StarLevelFieldState extends State<StarLevelField> {
  double select;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    select = widget.select;
  }

  @override
  Widget build(BuildContext context) {
    List<Widget> _list = new List();
    double val = select;
    for (int i=0;i<widget.quantity;i++){
      double select = 0;
      if (val > 0){
        select = val;
        val-=1;
      }

      _list.add(
          StarItemField(
            index: i,
            image: widget.img,
            size: widget.size,
            selected: widget.selected,
            unselected: widget.unselected,
            fill: widget.fill,
            select: select,
          )
      );
    }
    return NotificationListener<StarNotification>(
      onNotification: (notification){
        if (widget.tap){
          setState(() {
            select = notification.index;
            if (null != widget.controller)
              widget.controller.value.index=select;
          });
        }
        return true;
      },
      child: Row(
        children: _list,
      ),
    );
  }
}